package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-20
 * Time: 21:03
 */
public class ThreadDemon15_2 {
    private static long result = 0;
    public static void main(String[] args) throws InterruptedException {
        //用多线程去计算前多少个数字之和
        Thread thread = new Thread(()->{
            long tem = 0;
            for (long i = 0; i < 1_0000_0000L; i++) {
                tem += i ;
            }
            result += tem ;
        });

        thread.start();//创建一个新的线程，新的线程跟之前的线程是"并发执行"的关系
        long beg = System.currentTimeMillis();//记录开始时间
        //main函数线程阻塞，等待thread线程运行

        thread.join();

        long end = System.currentTimeMillis();//记录结束时间

        System.out.println("单线程的计算结果是：" + result);
        System.out.println("单线程用到的时间是:" + (end - beg) );


    }
}
